# -*- encoding:utf-8 -*-
"""
@作者：ven
@文件名：person.py
@时间：2021-09-03  11:43
@文档说明:
"""
import json
from pprint import pprint

import requests, base64
from requests_toolbelt import MultipartEncoder

from common.GetConfig import config_ip
from common.GetFilePath import filePath
from common.Request import Request


class Person():

    def person_add(self, data, test_api_login):
        '''
        :param data: 新增人员传入的json
        :param test_api_login: 获取登录token
        :return: post请求返回的结果
        '''

        return Request().post(url=config_ip() + '/workerInfos', data=json.dumps(data),
                              headers={'token': '{0}'.format(test_api_login),
                                       'Content-Type': 'application/json; charset=UTF-8',
                                       'X-Requested-With': 'XMLHttpRequest'})

    def person_edit(self, data, test_api_login):
        '''
        :param data: 人员id
        :param test_api_login:
        :return: 编辑人员
        '''

        return Request().put(url=config_ip() + '/workerInfos', data=json.dumps(data),
                             headers={'token': '{0}'.format(test_api_login),
                                      'Content-Type': 'application/json; charset=UTF-8',
                                      'X-Requested-With': 'XMLHttpRequest'})

    def person_edit_photo(self, data, photo_dir, test_api_login):
        '''
        :param data: 人员id
        :param test_api_login:{"workerId":"xx",
                               "photo":"base64"}
        :return: 编辑人员
        '''
        with open(r'{}'.format(photo_dir), "rb") as f:
        #with open(filePath('data/photo', 'ym.jpg'), "rb") as f:
            base64_data = str(base64.b64encode(f.read()), 'utf-8')
            #拼接请求体
            data["photo"] = base64_data
        request_body = MultipartEncoder(data)
        return Request().post(url=config_ip() + '/workerPhotos', data=request_body,
                             headers={'token': '{0}'.format(test_api_login),
                                      'Content-Type': request_body.content_type,
                                      'X-Requested-With': 'XMLHttpRequest'})

    def person_del(self, id, test_api_login):
        '''
        :param id:人员id
        :param test_api_login:
        :return:删除人员信息
        '''
        return Request().delete(url=config_ip() + '/workerInfos/{}'.format(id),
                                headers={'token': '{0}'.format(test_api_login),
                                         'Content-Type': 'application/json; charset=UTF-8',
                                         'X-Requested-With': 'XMLHttpRequest'})

    def person_sendWorker(self, data, test_api_login):
        '''
        :param data:
        :param test_api_login:
        :return: 下发人员至设备
        '''
        return Request().post(url=config_ip() + '/deviceTasks/sendWorker', data=data,
                              headers={'token': '{0}'.format(test_api_login),
                                       'Content-Type': data.content_type,
                                       'X-Requested-With': 'XMLHttpRequest'})

    def person_list(self, data, test_api_login):
        '''
        :param test_api_login:
        :return: 人员列表
        '''
        return Request().post(url=config_ip() + '/workerInfos/queryList', data=data,
                              headers={'token': '{0}'.format(test_api_login),
                                       'Content-Type': 'application/x-www-form-urlencoded',
                                       'X-Requested-With': 'XMLHttpRequest'})

    def person_importBio(self, data, test_api_login):
        '''
        :param data:生物特征json文件的地址
        :param test_api_login:
        :return: 导入生物特征json文件至系统里面
        '''
        return Request().post(url=config_ip() + '/workerInfos/importBio', data=data,
                              headers={'token': '{0}'.format(test_api_login),
                                       'Content-Type': data.content_type,
                                       'X-Requested-With': 'XMLHttpRequest'})

    def person_exportBio(self, data, test_api_login):
        '''
        :return: 导出人员信息，导出类型为json
        '''
        s = requests.Session()
        s.post(url=config_ip() + '/workerInfos/queryList', data={"start": 0, "length": 1000},
               headers={'token': '{0}'.format(test_api_login),
                        'Content-Type': 'application/x-www-form-urlencoded',
                        'X-Requested-With': 'XMLHttpRequest'})

        return s.post(url=config_ip() + '/workerInfos/exportBio', data=data,
                      headers={'token': '{0}'.format(test_api_login),
                               'X-Requested-With': 'XMLHttpRequest',
                               'Referer': config_ip() + '/pages/base/workerInfo/workerInfoList.html'})
